package com.example.carbonbackend.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.carbonbackend.common.BaseResponse;
import com.example.carbonbackend.common.ResultUtils;
import com.example.carbonbackend.service.OssUploadService;
import com.example.carbonbackend.utils.PathUtils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Service
@Data
@Slf4j
public class OssUploadServiceImpl implements OssUploadService {
    private String accessKey = "CJWTP5Q4W3_AKSWUe4lJdCJs9XGq4ys3pvhg8oU6";
    private String secretKey = "QVYpmLS-lmbgSZ-TnJx9fkS2rFIu48Q32IKa1F-m";
    private String bucket = "carbonxxz";

    @Override
    public BaseResponse uploadFile(MultipartFile img) throws IOException {
        String originalFileName = img.getOriginalFilename();
        Long fileSize = img.getSize();

//       if(fileSize>2*1024*1024)
//       {
//           throw new SystemException(AppHttpCodeEnum.FILE_SIZE_ERROR);
//       }

//       if(!originalFileName.endsWith(".png")&&!originalFileName.endsWith(".jpg"))
//       {
//           throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
//       }

        String fileName = PathUtils.generateFilePath(originalFileName);

        log.info(fileName);
        String url = uploadOss(img, fileName);

        return ResultUtils.success(url);
    }

    private String uploadOss(MultipartFile img, String filePath) throws IOException {

        //构造一个带指定 Region 对象的配置类。你的七牛云OSS创建的是哪个区域的，那么就调用Region的什么方法即可
        Configuration cfg = new Configuration(Region.huanan());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        UploadManager uploadManager = new UploadManager(cfg);
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png

        InputStream localFilePath = img.getInputStream();

//默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = filePath;
        System.out.println("key " + key);
        System.out.println("acc " + accessKey);
        Auth auth = Auth.create(accessKey, secretKey);
        System.out.println(666);
        String upToken = auth.uploadToken(bucket);
        System.out.println("upToken " + upToken);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken, null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);


            return "http://sb7hcahwr.hn-bkt.clouddn.com/" + key;
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);
                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        }

        return null;
    }

}
